<?php
/*
# ------------------------------------------------------------------------
# JA Job Board Package
# ------------------------------------------------------------------------
# Copyright (C) 2004-2010 JoomlArt.com. All Rights Reserved.
# @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
# Author: JoomlArt.com
# Websites: http://www.joomlart.com - http://www.joomlancers.com.
# ------------------------------------------------------------------------
*/

jimport( 'joomla.application.component.view' );

/**
 * @package Joomla
 * @subpackage JobBoard
 */
class JobBoardViewJAEmployers extends JView
{
    /**
     * Display the view
     */
    function display($tmpl=null)
    {
    	global $option;
    			
		switch ($this->getLayout()){	
			case 'account':	
				$this->displayAccount();
				break;
			case 'transactions':
				$this->displayTransactions();
				break;
			case 'invoices':
				$this->displayInvoices();
				break;
			case 'invoices_detail':
				$this->displayInvoice_Detail();
				break;
			case 'trans_detail':
				$this->viewTrans_detail();
				break;		
			case 'profile':	
			    $this->viewProfile();
				break;	
		}
		
		$user 	= JFactory::getUser();
		$account = getAccountByUserID($user->id);
		$this->assignRef('account', $account );
		
		parent::display($tmpl);
  	}
  	
  	function displayAccount($user_id=null)
  	{
  		global $mainframe;
  		if ($user_id==null)
  		{
  			$user = JFactory::getUser();
  			$user_id = $user->id;
  		}
  		if(!$user_id){
  			$mainframe->redirect(JRoute::_('index.php?option=com_user&task=login'));
  		}
  		$db = JFactory::getDBO();
  		$query = 'SELECT id FROM  #__ja_employers WHERE user_id='.$user_id;
  		$db->setQuery($query);
  		$cid = $db->loadObject();
  		if($cid){
  			JRequest::setVar('cid', $cid->id);
  		}
  		else JRequest::setVar('cid', 0);
  		$item = $this->get('Item');
  		
  		$this->assignRef('item', $item );
  	}
  	
  	function displayInvoice_Detail()
  	{
  		$user = JFactory::getUser();
		
  		$product_type = JRequest::getVar('product_type');
  		$customerDetail = getCustomerProfile($user->id);
  		$cid = JRequest::getInt('id', 0);
		
		$this->assignRef('customerDetail',$customerDetail);
		require_once JPATH_COMPONENT_SITE.DS.'models'.DS.'jatransactions.php';
		$transaction_model = new JobBoardModeljaTransactions();
		$item = $transaction_model->getItem();
		$this->assignRef('item', $item );		
  	}
  	
	function viewTrans_detail()
	{		
		global $mainframe,$option,$permission, $jbconfig;
		$paymentObject = get_payment_gateways();
		require_once JPATH_COMPONENT_SITE.DS.'models'.DS.'jatransactions.php';
		$transaction_model = new JobBoardModeljaTransactions();
		$item = $transaction_model->getItem();
		for ($i=0;$i<count($paymentObject);$i++)
			if ($paymentObject[$i]->id==$item->payment_method)
				$item->payment_method = $paymentObject[$i]->alias;  
		 //Payment method
		
  		$arr_posting_plan_type = JobBoardConstant::getPostingPlan_Type();  		
  		
		//Invoice status
		$arr_status = JobBoardConstant::getStatus_Invoice();
		$arr_product_type = array("S"=>"Subscription","P"=>"Posting Plan","C"=>"Credit");
		$this->setLayout('trans_detail');;
		$this->assignRef('config',$jbconfig);
		
		$this->assignRef('value_type',$arr_posting_plan_type);
		$this->assignRef('product_type',$arr_product_type);
		
		$this->assignRef('paysys', $paymentObject);
		$this->assignRef('item', $item);					
	}
  	
  	function displayInvoices()
  	{  		
  		$user = JFactory::getUser();
  		$product_type = JRequest::getVar('product_type');
  		
  		if ($product_type != null)
  			$_SESSION['product_type'] = $product_type;
  		else 
  			$product_type = $_SESSION['product_type'];
  		if ($product_type==null)
  			$product_type='s';
  		JRequest::setVar('product_type',$product_type);
  		$arr_invoicetype = array();
  		$arr_invoicetype[2]->value = 's';
  		$arr_invoicetype[2]->text = 'Subscription Invoice';
  		$arr_invoicetype[3]->value = 'p';
  		$arr_invoicetype[3]->text = 'Posting Plan Invoice';
  		$arr_invoicetype[1]->value = 'c';
  		$arr_invoicetype[1]->text = 'Credit Invoice';
  		$invoice_typeObject = JHTML::_('select.genericlist',   $arr_invoicetype, 'product_type', 'class="inputbox" size="1" onchange="adminForm.submit();"', 'value', 'text',$product_type);
  		$this->assignRef('invoice_typeObject',$invoice_typeObject);
  		
  		switch (strtolower($product_type))
  		{
  			case 's':
  				require_once JPATH_COMPONENT_SITE.DS.'models'.DS.'jasubscriptions_invoice.php';
  				$filter_subscription_invoice = array();
		  		$filter_subscription_invoice['user_id'] = $user->id;
		  		JRequest::setVar('filter_subscription_invoice',$filter_subscription_invoice);
		  		$subs_invoice_model = new JobBoardModelJASubscriptions_Invoice();
		  		$subscription_invoice_items = $subs_invoice_model->getItems();
		  		$pagination = $subs_invoice_model->getPagination();
		  		$this->assignRef('pagination', $pagination );
		  		$this->assignRef('items', $subscription_invoice_items );
		  		$filter_order = JRequest::getVar('filter_order');
  				$this->assignRef('filter_order',$filter_order);
		  		break;
  			case 'p':
  				require_once JPATH_COMPONENT_SITE.DS.'models'.DS.'japostingplans_invoice.php';
		  		//Get posting plan order history
		  		$filter_postingplan_invoice = array();
		  		$filter_postingplan_invoice['user_id'] = $user->id;
		  		JRequest::setVar('filter_postingplan_invoice',$filter_postingplan_invoice);
		  		$plan_invoice_model = new JobBoardModelJAPostingPlans_Invoice();
		  		$postingplan_invoice_items = $plan_invoice_model->getItems();
		  		$pagination = $plan_invoice_model->getPagination();
		  		$this->assignRef('pagination', $pagination );
		  		$this->assignRef('items', $postingplan_invoice_items );
		  		$filter_order = JRequest::getVar('filter_order');
  				$this->assignRef('filter_order',$filter_order);		  		
  				
  				break;
  			case 'c':
  				require_once JPATH_COMPONENT_SITE.DS.'models'.DS.'jacredits_invoice.php';
		  		//Get posting plan order history
		  		$filter_credit_invoice = array();
		  		$filter_credit_invoice['user_id'] = $user->id;
		  		JRequest::setVar('filter_credit_invoice',$filter_credit_invoice);
		  		$credit_invoice_model = new JobBoardModelJACredits_Invoice();
		  		$credit_invoice_items = $credit_invoice_model->getItems();
		  		$pagination = $credit_invoice_model->getPagination();
		  		$this->assignRef('pagination', $pagination );		  		
		  		$this->assignRef('items', $credit_invoice_items );
		  		$filter_order = JRequest::getVar('filter_order');
  				$this->assignRef('filter_order',$filter_order);		  		
  				break;
  		}

  	}
  	
  	/**
  	 * Display Account Information
  	 */
  	function displayTransactions()
  	{
  		require_once JPATH_COMPONENT_SITE.DS.'models'.DS.'jatransactions.php';
  		$user = JFactory::getUser();
  		JRequest::setVar('userid',$user->id);  		  		
		
  		//Get transactions history
  		$transaction_model = new JobBoardModeljaTransactions();
  		$transaction_items = $transaction_model->getItems();
  		$pagination = $transaction_model->getPagination();
//  		$filter_order = JRequest::getVar('filter_order');
  		$filter_order = array();
		$filter_order['order'] = JRequest::getVar('filter_order');
		$filter_order['order_dir'] = JRequest::getVar('filter_order_Dir');
		if (!$filter_order['order'])
		{
			$filter_order['order'] = "a.created_date";
			$filter_order['order_dir'] = "DESC";
		}
		if ($filter_order['order']=='product_name')
		{
			$pretemp_items = array();
			$temp_items = array();
			for ($i=0;$i<count($transaction_items);$i++)
				$pretemp_items[$transaction_items[$i]->product_name] = $transaction_items[$i];
			if (strtolower($filter_order['order_dir']) == "desc")
				krsort($pretemp_items);
			else 
				ksort($pretemp_items);
			foreach ($pretemp_items as $k=>$value)
				$temp_items[] = $value;
			$transaction_items = $temp_items;
		}
		$this->assignRef('filter_order',$filter_order);  		
  		$this->assignRef('pagination', $pagination);
  		$this->assignRef('items', $transaction_items );  		  		
  		
  	}
  	
  	function viewProfile(){
  	     include_once(JPATH_COMPONENT_SITE.DS.'models'.DS.'japrofiles.php');
  	     $model = new JobBoardModelJAProfiles();
  	     include_once(JPATH_COMPONENT_SITE.DS.'views'.DS.'japrofiles'.DS.'view.html.php');
  	     JobBoardViewJAProfiles::view($model);
  	}
  	
  	function menu()
  	{
  		global $option;
  		?>
  			
			<div class="moduletable">
			
				<h3><?php echo JText::_('My Cpanel');?></h3>
				<ul>
					<li>
						<a href="index.php?option=com_jajobboard&view=jaemployers&layout=account">
							<?php echo JText::_('Account Info');?>
						</a>
					</li>
					<li>
						<a href="index.php?option=com_jajobboard&view=jaemployers&layout=userdetails">
							<?php echo JText::_('Membership Details');?>
						</a>
					</li>
					<li>
						<a href="index.php?option=com_jajobboard&view=jaemployers&layout=profile">
							<?php echo JText::_('My Profile');?>
						</a>
					</li>
					<li>
						<a href="index.php?option=com_jajobboard&view=jaemployers&layout=transactions">
							<?php echo JText::_('Transactions History');?>
						</a>
					</li>
					
				</ul>
			
			</div>
			
  		<?php
  	}
  	
  	function account_info_static($acc_item){
  		global $jbconfig;
  		?>

			<div class="moduletable">
					<h3>
				        <?php echo JText::_('Account balance')?>
				  </h3>
			    <table class="m_table">
			    	
			        <?php if($jbconfig['currency']->get('mode')=='special_currency'){?>
					<tr>
						<td width="70%">
							<?php echo JText::_('Credit balance')?>:
						</td>
						<td width="30%">
							<?php echo $acc_item->credit_balance; ?>
						</td>
					</tr>
					<?php }?>
					<tr>
						<td width="70%">
							<?php echo JText::_('Standard posting left')?>:
						</td>
						<td width="30%">
							<?php echo $acc_item->standard_posts_balance; ?>
						</td>
					</tr>
					<tr>
						<td width="70%">
							<?php echo JText::_('Premium posting left')?>:
						</td>
						<td width="30%">
							<?php echo $acc_item->premium_posts_balance; ?>
						</td>
					</tr>
				</table>
				<table class="m_table">
					<h3>
				        <?php echo JText::_('Search resume');?>
				  </h3>
						<td width="40%">
							<?php echo JText::_('Start date')?>:
						</td>
						<td width="60%">
							<?php
								if(($acc_item->begin_date_search_resume=='') || ($acc_item->begin_date_search_resume==null) || ($acc_item->begin_date_search_resume=='0000-00-00 00:00:00')) echo "N/A"; 
								else echo date(str_replace('%', '', $jbconfig['general']->get('format_date', '%Y-%m-%d')), strtotime($acc_item->begin_date_search_resume));
							?>
						</td>
					</tr>
					<tr>
						<td width="40%">
							<?php echo JText::_('End date')?>:
						</td>
						<td width="60%">
							<?php 
								if(($acc_item->end_date_search_resume=='') || ($acc_item->end_date_search_resume==null) || ($acc_item->end_date_search_resume=='0000-00-00 00:00:00')) echo "N/A"; 
								else echo date(str_replace('%', '', $jbconfig['general']->get('format_date', '%Y-%m-%d')), strtotime($acc_item->end_date_search_resume));
							?>
						</td>
					</tr>
	
			    </table>
			</div>
			
			
			
  		<?php
  	}
  	  	
}
?>